package router

import (
	"context"
	"honeyweb/log"
	"net/http"
	"time"
)

var (
	HttpSrv *http.Server
)

const (
	DefaultAddr = ":8080"
)

func HttpServertRun() {
	r := InitRouter()
	HttpSrv = &http.Server{
		Addr: DefaultAddr,
		Handler: r,
	}

	go func() {
		if err := HttpSrv.ListenAndServe(); err != nil {
			log.S().Error("http run err: ", err)
		}
	}()
}

func HttpServerStop() {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	if err := HttpSrv.Shutdown(ctx); err != nil {
		log.S().Error("http stop err: ", err)
	}
	log.S().Debug("http server sttoped!")
}
